package com.loit.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loit.business.entity.ReqComVO;
import com.loit.business.entity.User;
import com.loit.business.dao.UserDao;
import com.loit.business.service.UserService;
import com.loit.loitcommon.json.AjaxJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@RequestMapping(value = "/api/v1/user")
@Api(tags = "[用户信息（测试）]接口",  value = "UserController")
public class UserController {

    /**
     * 日志工具
     */
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserDao userDao;
    
    @Autowired
    private UserService userService;

    @ApiOperation(value = "测试测试", notes = "测试测试")
    @ApiImplicitParam(paramType = "query", name = "num", value = "数字", required = false, dataType = "Integer")
    @GetMapping("/queryByNum")
    public int queryByNum(@PathParam("num") int num){
        return num;
    }

    @ApiOperation(value = "查询用户信息列表", notes = "查询用户信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userName", value = "用户名", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "age", value = "年龄", required = false, dataType = "Integer")
    })
    @GetMapping("/queryMyUserList")
    public List<User> queryMyUserList(@ApiIgnore User requestUser){
        logger.info(requestUser.toString());
        List<User> userList = userDao.queryUserList();
        for(User user:userList){
            System.out.println(user);
        }
        return userList;
    }


    @ApiOperation(value = "根据id查询用户信息", notes = "根据id查询用户信息")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id", required = true, dataType = "Integer")
    @GetMapping("/queryUserById")
    public User queryUserById(@PathParam("id") int id){
        User user = userDao.queryUserById(id);
        return user;
    }


    @ApiOperation(value = "分页测试demo1", notes = "分页测试demo1")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "current", value = "当前页码", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "size", value = "分页大小", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "userName", value = "用户名", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "age", value = "年龄", required = false, dataType = "Integer")
    })
    @RequestMapping(value = "/pagetest1", method = RequestMethod.GET)
    public IPage<User> pagetest1(@ApiIgnore User requestUser, Long current, Long size) {
        // 简单分页查询
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("age", requestUser.getAge());
        IPage<User> userEntityIPage = userService.pageByAge(new Page<>(current, size), queryWrapper);
        System.out.println("总页数： " + userEntityIPage.getPages());
        System.out.println("总记录数： " + userEntityIPage.getTotal());
        userEntityIPage.getRecords().forEach(System.out::println);
        return userEntityIPage;
    }


    @ApiOperation(value = "分页测试demo2", notes = "分页测试demo2")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "current", value = "当前页码", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "size", value = "分页大小", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "userName", value = "用户名", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "age", value = "年龄", required = false, dataType = "Integer")
    })
    @RequestMapping(value = "/pagetest2", method = RequestMethod.GET)
    public IPage<User> pagetest2(@ApiIgnore User requestUser, Long current, Long size) {
        // 模拟复杂分页查询
        IPage<User> userEntityIPage = userService.selectUserByAge(new Page<>(current, size), requestUser.getAge());
        System.out.println("总页数： " + userEntityIPage.getPages());
        System.out.println("总记录数： " + userEntityIPage.getTotal());
        userEntityIPage.getRecords().forEach(System.out::println);
        return userEntityIPage;
    }

    /**
	 * 详情
	 */
	@ApiOperation(value = "详情", notes = "根据id获取详情信息")
	@ApiImplicitParam(paramType = "path", name = "id", value = "id", required = true, dataType = "String")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public AjaxJson get(@PathVariable("id") Integer id) {
		User user = userService.getById(id);
		AjaxJson json = new AjaxJson();
		json.put("obj", user);
		return json;
	}

	/**
	 * 保存
	 */
	@ApiOperation(value = "保存", notes = "保存")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public AjaxJson save(@RequestBody User user) {
		AjaxJson json = new AjaxJson();
		try {
            userService.insert(user);
			json.setMsg("保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxJson.returnExceptionInfo("保存异常");
		}
		return json;
	}


    @ApiOperation(value = "糊涂测试", notes = "糊涂测试")
    @RequestMapping(value = "/hutooTest", method = RequestMethod.POST)
    public AjaxJson hutooTest(ReqComVO comVO) {
        AjaxJson json = new AjaxJson();
        try {
            System.out.println("comVO:"+comVO);
            json.setMsg("糊涂测试成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxJson.returnExceptionInfo("糊涂测试异常");
        }
        return json;
    }



}
